package foo.workLlog.bo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import foo.dao.ProjDAO;
import foo.dao.WorkLogDAO;
import foo.dbvo.WbsDbvo;
import foo.websystem.action.Consts;
import foo.workLlog.vo.AjaxAutoCompleteReturnVvo;
import foo.workLlog.vo.WorkLogVvo;

@Service
public class WorkLogBO {
	@Autowired
	WorkLogDAO workLogDAO;

	public List<WorkLogVvo> findWorkLog(String startDate, String endDate,
			String worker) throws Exception {

		Date endDt = Consts.DATE_FORMATER.parse(endDate);
		Date startDt = Consts.DATE_FORMATER.parse(startDate);
		Collection<String> workers = new ArrayList<String>();
		if (StringUtils.isBlank(worker)) {
			workers.add(worker);
		}
		List<WorkLogVvo> worklogs = workLogDAO.findWorkLog(startDt, endDt,
				workers);
		return worklogs;
	}

	public void insertWorkLog(String workDate, String wbsSn, String content,
			String hr, String worker) throws Exception {
		if (StringUtils.isBlank(wbsSn) || StringUtils.isBlank(workDate)
				|| StringUtils.isBlank(hr)) {
			throw new NullPointerException("必填欄位為空");
		}
		Integer wbsSnInt = Integer.valueOf(wbsSn);
		Integer hrInt = Integer.valueOf(hr);
		if (hrInt.intValue() == 0) {
			throw new NullPointerException("工時為0?");
		}
		Date workDateDt = Consts.DATE_FORMATER.parse(workDate);
		workLogDAO.insertWorLog(workDateDt, wbsSnInt, content, hrInt, worker);
	}

	public void updateWorkLog(String workLogSn, String workDate, String wbsSn,
			String workLogContent, String hr) throws Exception {
		workLogDAO.updateWorkLog(Integer.valueOf(workLogSn),
				Consts.DATE_FORMATER.parse(workDate), Integer.valueOf(wbsSn),
				workLogContent, null, Integer.valueOf(hr));
	}

	@Deprecated
	public ArrayList<AjaxAutoCompleteReturnVvo> findPickWbsTaskNames(
			String searchWbsName) {
		List<Map<String, Object>> queryResult = workLogDAO
				.findPickWbsTaskNames(searchWbsName);
		ArrayList<AjaxAutoCompleteReturnVvo> searchResult = new ArrayList<AjaxAutoCompleteReturnVvo>();
		for (Map<String, Object> map : queryResult) {
			AjaxAutoCompleteReturnVvo vo = new AjaxAutoCompleteReturnVvo();
			StringBuilder sb = new StringBuilder();
			Object _s = map.get("projName");
			sb.append(_s == null ? "" : _s);
			sb.append("/");
			_s = map.get("wbsName");
			sb.append(_s == null ? "" : _s);
			sb.append("/");
			_s = map.get("wbsContent");
			sb.append(_s == null ? "" : _s);
			String s = sb.toString();
			vo.setData(s);
			vo.setValue(s);
			searchResult.add(vo);
		}
		logger.debug("取得候選任務名稱:" + searchWbsName + "->" + searchResult);
		return searchResult;
	}

	public List<WbsDbvo> findWbsByQuery(String projSn, String wbsName,
			String wbsTypeId) {
		Integer projSnInt = StringUtils.isBlank(projSn) ? null : Integer
				.valueOf(projSn);
		List<WbsDbvo> wbses = projDAO.findWbsByQuery(wbsName, wbsTypeId,
				projSnInt);
		return wbses;

	}

	@Autowired
	private ProjDAO projDAO;

	private Logger logger = Logger.getLogger(getClass());

}
